﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hegang_winform.dao.impl
{
    class MySqlObject : DbObject
    {
        private static MySqlConnection getConnection()
        {
            string server="127.0.0.1";
            string port="3306";
            string user="root";
            string password= "wlzx^^*668install";
            string database="hfd";
            string str = string.Format("server = {0}; port = {1}; user = {2}; password = {3}; database = {4};",server,port,user,password,database);
            MySqlConnection conn = null;
            try
            {
                conn = new MySqlConnection(str);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return conn;
        }

        public DataTable cmdHasReturn(string sql)
        {
            MySqlConnection conn = null;
            try
            {
                conn = getConnection();
                conn.Open();
                MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }

        }

        public void cmdNoReturn(string sql)
        {
            MySqlConnection conn = null;
            try
            {
                conn = getConnection();
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                if(conn!=null)
                    conn.Close();
            }
        }
    }
}
